package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneAccessProdInstService;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneCommonService;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inst.IProdInstService;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.batch.attr.BatchAttrUtil;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import com.ztesoft.bss.base.staticdata.ServiceOfferConst;
import com.ztesoft.bss.base.util.LocalAttrUtils;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class OpenWayHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    public IProdInstService prodInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Autowired
    private IQuerySceneAccessProdInstService querySceneProdInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        IQuerySceneService querySceneService = SpringUtil.getBean(IQuerySceneService.class);
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteSceneInst sceneInst = querySceneService.getSceneInst(sceneInstId);
        String serviceOfferId = sceneInst.getServiceOfferId();
        String postProvinceNbr = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String cateId = CuccPosServiceUtils.getCateId(sceneInstId, KiteWebContext.getContext().getCustId());
        String bizType = querySceneInstService.getExtParameterValue(sceneInstId, "bizType");

        //核查追单隐藏
        if(KiteStringUtils.equals("ORD10005", commonAttr.getFieldName())&&KiteStringUtils.equalsAny(cateId, ProdCateUtil.getDoubleLineCateIdForArr())&&  KiteStringUtils.equals("1014", serviceOfferId)&&KiteStringUtils.equals("1012", bizType)){
            commonAttr.setShowType("E");
            //commonAttr.getAttrValues().removeIf(p -> KiteStringUtils.equalsAny( p.getAttrValue(),"1","2"));

            // 再把修改的状态写入缓存实例中
            KiteAttr attr = querySceneInstService.getCommonAttr(sceneInstId, commonAttr.getFieldName());
            if (Objects.isNull(attr)) {
                return;
            }
            attr.setShowType("E");
            operateSceneInstService.setCommonAttr(sceneInstId, attr);
        }
        if(KiteStringUtils.equals(LocalKeyConsts.PROVINCE_GUANGDONG,postProvinceNbr)){
            // 广东光纤出租默认“先核查再开通”，不展示“无需核查直开”
            List<StaticAttrDO> attrValues = commonAttr.getAttrValues();
            attrValues.removeIf(attrValue -> KiteStringUtils.equals("0", attrValue.getAttrValue()) && KiteStringUtils.equals(ProdCateUtil.getSingleCateId(ProdCateUtil.GXCZ_CODE), cateId));
            // 把实例里的数据修改
            KiteAttr openModeAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10005");
            List<AttrValue> attrValueList = AttrValueCache.get(commonAttr.getAttrId());
            if (Objects.nonNull(openModeAttr) && KiteListUtils.isNotEmpty(attrValueList)) {
                attrValueList.removeIf(attrValue -> KiteStringUtils.equals("0", attrValue.getAttrValue()) && KiteStringUtils.equals(ProdCateUtil.getSingleCateId(ProdCateUtil.GXCZ_CODE), cateId));
                openModeAttr.setAttrValues(KiteBeanUtils.copyPropsForList(attrValueList, KiteAttrValue.class));
                operateSceneCommonService.setCommonAttr(sceneInstId, openModeAttr);
            }
        }


        // 开通方式-补录资料，仅在订单性质=补录单时展示
        String orderNature = querySceneInstService.getCommonAttrValue(sceneInstId, LocalKeyConsts.ORDER_NATURE);
        if(!KiteStringUtils.isEqual(orderNature, "2")) {
            commonAttr.getAttrValues().removeIf(attr -> KiteStringUtils.equals(attr.getAttrValue(), "4"));
            KiteAttr openModeAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.OPEN_WAY);
            openModeAttr.getAttrValues().removeIf(attr -> KiteStringUtils.equals(attr.getAttrValue(), "4"));
            operateSceneCommonService.setCommonAttr(sceneInstId, openModeAttr);
        }

        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode");
        String initFlag = querySceneInstService.getExtParameterValue(sceneInstId, "openWayInited");
//        河南订购、变更默认为”无需核查直开“
        if(KiteStringUtils.equals(LocalKeyConsts.PROVINCE_HENAN,postProvinceNbr) && KiteStringUtils.equalsAny(serviceOfferId,"1","2826")
            && KiteStringUtils.isEmpty(nodeCode) && !LocalKeyConsts.IFTRUE_T.equals(initFlag)) {
            commonAttr.setValue("0");
            commonAttr.setDefaultValue("0");
            commonAttr.setValueDesc("无需核查直开");
            setCommonAttr(commonAttr);
            operateSceneInstService.putExtParameter(sceneInstId, "openWayInited",  "T");
        }

        // 签订、追单、五大双线-业务开通-重提  不允许修改开通方式
        if (("OrderSigned".equals(nodeCode) || LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW.equals(serviceOfferId)
            || (KiteStringUtils.isEqual(serviceOfferId, ServiceOfferConsts.SERVICE_OFFER_ORDER) && KiteStringUtils.isNotEmpty(nodeCode) && KiteStringUtils.equalsAny(cateId, ProdCateUtil.getDoubleLineCateIdForArr())))
            && KiteStringUtils.isEqual("T", commonAttr.getIsEdit())) {
            commonAttr.setIsEdit("F");
            setCommonAttr(commonAttr);
        }
        BatchAttrUtil.batchOpenWayHandle(sceneInstId, serviceOfferId, commonAttr);
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        // 无需核查原因
        KiteAttr reasonAttr = querySceneInstService
            .getCommonAttrsByFieldName(sceneInstId, "ORD10015", "93");
        if (Objects.isNull(reasonAttr)) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        // 当开通方式选择“无需核查直开”的时候显示 无需核查原因
        if (KiteStringUtils.isNotEmpty(value)) {
            if (KiteStringUtils.equals("0", value)) {
                reasonAttr.setShowType("D");
                operateSceneCommonService.setCommonAttr(sceneInstId, reasonAttr);
            } else {
                reasonAttr.setShowType("H");
                operateSceneCommonService.setCommonAttr(sceneInstId, reasonAttr);
            }
        } else {
            // 回填或者开通方式初始化为“无需核查直开”时，value为空，需显示无需核查原因
            KiteAttr openWayAttr = querySceneInstService.getCommonAttr(sceneInstId, fieldName);
            if (Objects.nonNull(openWayAttr)) {
                if (KiteStringUtils.equals("0", openWayAttr.getValue())) {
                    reasonAttr.setShowType("D");
                } else {
                    reasonAttr.setShowType("H");
                }
                operateSceneCommonService.setCommonAttr(sceneInstId, reasonAttr);
            }
        }
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String serviceOfferId = sceneInst.getServiceOfferId();
        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode");
        // 开通方式
        KiteAttr openWayAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10005");
        // 追单不允许修改开通方式
        if (Objects.nonNull(openWayAttr)) {
            // 签订、追单不允许修改开通方式
            if (("OrderSigned".equals(nodeCode) || LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW.equals(serviceOfferId))
                && KiteStringUtils.isEqual("T", openWayAttr.getIsEdit())) {
                openWayAttr.setIsEdit("F");
                operateSceneCommonService.setCommonAttr(sceneInstId, openWayAttr);
            }
        }
//        跨域优化
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String cateId = CuccPosServiceUtils.getCateId(sceneInstId, kiteSceneInst.getCustId());
        // 以太网、SDH国际6个产品不根据跨域设置  核查方式
//        if(!KiteStringUtils.equalsAny(cateId, "2101140","2101142","2101141","2101121","2101122","2101123")) {
//            // 跨域优化
//            KiteProdInst prodInst = querySceneInstService.listProdInstsFromOfferInst(kiteSceneInst.getCustId(), kiteSceneInst.getRootInstId()).stream()
//                .filter(inst -> !KiteStringUtils.equalsAny(inst.getActionType().name(), ActionType.X.name(), ActionType.D.name()))
//                .findFirst().orElse(null);
//            String mainServiceOfferId = querySceneInstService.getSceneInst(sceneInstId).getServiceOfferId();
//            KiteAttr checkWayAttr = SpringUtil.getBean(IQuerySceneCommonService.class).getCommonAttr(sceneInstId, "ORD10222").getResultObject();
//            if(null==checkWayAttr){
//                return super.apply(sceneInstId, fieldName, value, valueDesc);
//            }
//            boolean isCrossDomain = LocalAttrUtils.isCrossDomain(kiteSceneInst, prodInst);
//            LocalAttrUtils.ORD10222(isCrossDomain,mainServiceOfferId,checkWayAttr,value,prodInst.getProdId());
//            operateSceneCommonService.setCommonAttr(sceneInstId, checkWayAttr);
//        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
